<? 
/* Include Files *********************/
session_start(); 
include("database.php");
include("login.php");
$input=$_GET['team'];
$refresh=$_GET['refresh'];
if($refresh)
{
	header('Refresh: ' . $refresh . '');
}
/*************************************/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../../../../main/js/modules/sorttable.js"></script>
<title>Spyder Scouting | View Specific Team</title>
<link href="http://teamspyder.org/assets/css/scouting.css" rel="stylesheet" type="text/css" />
<!-- 1. Add these JavaScript inclusions in the head of your page -->
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
		<script type="text/javascript" src="../../../../main/js/modules/js/highcharts.js"></script>
		
		<!-- 1a) Optional: add a theme file -->
		<!--
			<script type="text/javascript" src="./js/themes/gray.js"></script>
		-->
		
		<!-- 1b) Optional: the exporting module -->
		<script type="text/javascript" src="../../../../main/js/modules/js/modules/exporting.js"></script>
		
		
		<!-- 2. Add the JavaScript to initialize the chart on document ready -->
		<?
						mysql_connect("localhost","scouting","phsr2010");
						mysql_select_db('spyderweb') or die(mysql_error());
								   
						
							
							
							
							
						
						
						#arsort($values);
						?>
        <script type="text/javascript">
		
			var chart;
			$(document).ready(function() {
				chart = new Highcharts.Chart({
					chart: {
						backgroundColor: "#CCCCCC",
						renderTo: 'chart_container',
						//defaultSeriesType: 'column',
						margin: [ 50, 50, 100, 80]
					},
					credits: {
						enabled: false
					},
					title: {
						text: 'Team <? echo $input; ?>'
					},
					xAxis: {
						tickmarkPlacement: "between",
						tickLength: 5,
						categories: [
							<?
							$query="SELECT scored, penalty, time, matchnum FROM scouting2011 WHERE team='".$input."' AND regional='".$_SESSION['regional']."'";
							$result=mysql_query($query);
							if (!$result) {
die("I JUST GOT A BLEEDIN QUERY ERROR!!!!!<br />Query is: $query<br />Error is: ".mysql_error());
}				

							while($results = mysql_fetch_array($result))
							{
							echo "'Match " . $results['matchnum'] . "',";
							}
							?>
						]
					},
					tooltip: {
					formatter: function() {
						var s;
						if (this.point.name) { // the pie chart
							s = ''+
								this.point.name +': '+ this.y +' points';
						} else {
							s = ''+
								'Match '+this.x  +': '+ this.y +' '+this.series.name;
						}
						return s;
					}
				},
				yAxis: {
						min: 0,
						title: {
							text: 'Points'
						}
					},
					legend: {
						enabled: true
				},
				plotOptions: {
						column: {
							pointPadding: 0.1,
							borderWidth: 1,
							groupPadding: 0.001
								}
				},
					
				        series: [{
						type: 'column',
						name: 'Pts Scored',
						data: [
						<?
						$query="SELECT scored, bonus, penalty, time, matchnum FROM scouting2011 WHERE team='".$input."' AND regional='".$_SESSION['regional']."'";
							$result=mysql_query($query);
							if (!$result) {
die("I JUST GOT A BLEEDIN QUERY ERROR!!!!!<br />Query is: $query<br />Error is: ".mysql_error());
}				

							while($results = mysql_fetch_array($result))
							{
								$total=$results['scored']+$results['bonus'];
						echo $total . ",";
							}
							?>]
						}, {
						type: 'column',
						name: 'Penalty Pts',
						data: [
						<?
						$query="SELECT scored, penalty, time, matchnum FROM scouting2011 WHERE team='".$input."' AND regional='".$_SESSION['regional']."'";
							$result=mysql_query($query);
							if (!$result) {
die("I JUST GOT A BLEEDIN QUERY ERROR!!!!!<br />Query is: $query<br />Error is: ".mysql_error());
}				

							while($results = mysql_fetch_array($result))
							{
						echo "" . $results['penalty'] . ",";
							}
							?>]
						}, {
						type: 'column',
						name: 'Minibot Pts',
						data: [
						<?
						$query="SELECT scored, penalty, time, matchnum FROM scouting2011 WHERE team='".$input."' AND regional='".$_SESSION['regional']."'";
							$result=mysql_query($query);
							if (!$result) {
die("I JUST GOT A BLEEDIN QUERY ERROR!!!!!<br />Query is: $query<br />Error is: ".mysql_error());
}				

							while($results = mysql_fetch_array($result))
							{
						echo "" . $results['time'] . ",";
							}
							?>]	
					}]
				});
				
				
			});
		</script>
</head>
<body>
<div class="main"> 
	<div class="top">
		<div class="nav">
			<a href="../../../../main/js/modules/index.html"> Home </a> <l>|</l>
			<a href="../../../../main/js/modules/inputform.php"> Input Data </a> <l>|</l>
			<a href="../../../../main/js/modules/view.html"> View Data </a> <l>|</l>
			<a href="../../../../main/js/modules/edit.html"> Edit Data </a> <l>|</l>
			<a href="../../../../main/js/modules/predict.html"> Match Predictor </a> <l>|</l>
			<a href="../../../../main/js/modules/scoutlogin.php"> Login </a> <l>|</l>
			<a href="../../../../main/js/modules/logout.php"> Logout </a>
		</div>  
	</div>
	<div class="content">

<?php
if($logged_in){
$username="scouting"; //connection information being stored to variables
$password="phsr2010"; //  |                |
$database="spyderweb";//  |________________|
$input=$_GET['team'];

mysql_connect(localhost,$username,$password); //tells script to open a connection to the database
@mysql_select_db($database) or die( "Unable to select database"); //tells code ot select the database stored to $database, if fail, then script quits

$query="SELECT * FROM scouting2011 WHERE team='$input' AND regional='$_SESSION[regional]'"; //stored to variable query, code to select all data (the * means all) from table "scouting" and order by matchnum number
$result=mysql_query($query); //perform a query using the string defiend above
$num_rows = mysql_num_rows($result);

if($num_rows == 0){
	echo "No matches have been entered for that team" . "<br>" . "<a href=\"view.html\">" . "Click to go back." . "</a>";
} else {
	echo "<table align=\"center\" class=\"sortable\" border=\"1\" cellspacing=\"2\" cellpadding=\"2\">";
	echo "<tr>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Match" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Team" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Autonomous" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "uber tube" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Points Scored" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Bonus Points" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Minibot Place" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Card" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Penalty Points" . "</font>" . "</th>";
	echo "<th>" . "<font face=\"Arial, Helvetica, sans-serif\">" . "Comment" . "</font>" . "</th>";
	echo "</tr>";
	
	While($row = mysql_fetch_array($result))
		{
		echo "<tr>"; //starts a new row to the table
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['matchnum'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['team'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['autonomous'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['uber'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['scored'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['bonus'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['time'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['card'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['penalty'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "<td>" . "<font face=\"Arial, Helvetica, sans-serif\">" . $row['comment'] . "</td>"; //Creates a table cell and prints the given variable to it
		echo "</tr>"; //ends row in table
		}
	
	$query="SELECT SUM(autonomous), SUM(scored), SUM(uber), SUM(bonus), SUM(time) FROM scouting2011 WHERE team='$input' AND regional='$_SESSION[regional]'";
	$result=mysql_query($query); //perform a query using the string defiend above
	if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
	}
	$arr=mysql_fetch_row($result);
	$sum_auto=$arr[0];
	$sum_scored=$arr[1];
	$sum_uber=$arr[2];
	$sum_bonus=$arr[3];
	$sum_time=$arr[4];
	echo "<tr>" . "<td>" . "<b>" . "Totals" . "</b>" . "</td>" . "</tr>";
	echo "<tr>" . "<td>" . "</td>" . "<d>" . "</td>";
	echo "<td>" . "</td>";
	echo "<td>" . $sum_auto . "</td>";
	echo "<td>" . $sum_uber . "</td>";
	echo "<td>" . $sum_scored . "</td>";
	echo "<td>" . $sum_bonus . "</td>";
	echo "<td>" . $sum_time . "</td>";
	echo "<td>" . "</td>";
	echo "<td>" . "</td>";
	echo "<td>" . "</td>";
	echo "</tr>";

	$query="SELECT AVG(autonomous), AVG(scored), AVG(uber), AVG(bonus), AVG(time) FROM scouting2011 WHERE team='$input' AND regional='$_SESSION[regional]'";
	$result=mysql_query($query); //perform a query using the string defiend above
	$arr=mysql_fetch_row($result);
	$avg_auto=round($arr[0], 1);
	$avg_scored=round($arr[1], 1);
	$avg_uber=round($arr[2],1);
	$avg_bonus=round($arr[3],1);
	$avg_time=round($arr[4],1);
	echo "<tr>" . "<td>" . "<b>" . "Averages" . "</b>" . "</td>" . "</tr>";
	echo "<tr>" . "<td>" . "</td>" . "<td>" . "</td>";
	echo "<td>" . $avg_auto . "</td>";
	echo "<td>" . $avg_uber . "</td>";
	echo "<td>" . $avg_scored . "</td>";
	echo "<td>" . $avg_bonus . "</td>";
	echo "<td>" . $avg_time . "</td>";
	echo "<td>" . "</td>";
	echo "<td>" . "</td>";
	echo "<td>" . "</td>";
	echo "</tr>";
	echo "</table>"; //ends table
	?>
	
	<div id="chart_container" style="width: 1024px; height: 400px;"><chart></div>
	
	
	
	<?
}
mysql_close(); //close connection to database once everything else is done
}else{
 displayLogin();
 }
?>	
    </div>

</div>
</body></html>
